#define MAX_NODE_SIZE 3000
#define MAX(a, b) ((a) > (b) ? (a) : (b))

typedef struct {
    struct TreeNode *node;
    unsigned long long index;
} Pair;

int widthOfBinaryTree(struct TreeNode* root){
    unsigned long long res = 1;
    Pair * arr = (Pair *)malloc(sizeof(Pair) * MAX_NODE_SIZE);
    Pair * tmp = (Pair *)malloc(sizeof(Pair) * MAX_NODE_SIZE);
    int arrSize = 0, tmpSize = 0;
    arr[arrSize].node = root;
    arr[arrSize].index = 1LL;
    arrSize++;
    while (arrSize > 0) {
        tmpSize = 0;
        for (int i = 0; i < arrSize; i++) {
            if (arr[i].node->left) {
                tmp[tmpSize].node = arr[i].node->left;
                tmp[tmpSize].index = arr[i].index * 2;
                tmpSize++;
            }
            if (arr[i].node->right) {
                tmp[tmpSize].node = arr[i].node->right;
                tmp[tmpSize].index = arr[i].index * 2 + 1;
                tmpSize++;
            }
        }
        res = MAX(res, arr[arrSize - 1].index - arr[0].index + 1);
        arrSize = tmpSize;
        Pair * p = arr;
        arr = tmp;
        tmp = p;
    }
    return res;
}
